<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>safeModel</title>
</head>
<body>
<script>
    /*是不是经常忘记写new关键字？有没有办法保证在没使用new的时候也能生成实例？*/
    //安全模式帮助你搞定一切
    //先看看一般的类
    var Book =function(id){
        this.id = id;
    }
    var book = Book(1);
    console.log(book); //undefined

    //再来使用安全模式
    var Book_safe = function(id){
        if(this instanceof Book_safe){
            this.id = id;
        }else{
            return new Book_safe(id);
        }
    }

    var book2 = Book_safe(2);
    console.log(book2);
    //成功输出了实例，妈妈再也不用担心我忘记new了
    
</script>
</body>
</html>